﻿//-----------------------------------------------------------------------
// <copyright file="PivotCollectionBuilder.cs" company="Forebrain Technology, LLC">
//
// Copyright (c) Forebrain Technology, LLC. All rights reserved.
// 
// This source code is released under the Microsoft Public License (Ms-PL).
//
// </copyright>
//-----------------------------------------------------------------------
namespace PivotCollectionTools.Library
{
    using System.Collections.Generic;
    using System.IO;
    using System.Security.Permissions;
    using PivotCollectionTools.Data;
    using PivotCollectionTools.Xml;

    /// <summary>
    /// Used to create Pivot Collections for a given <see cref="PivotTable"/>
    /// </summary>
    public class PivotCollectionBuilder
    {
        /// <summary>
        /// Storage for the table to build a pivot collection from.
        /// </summary>
        private PivotTable table;

        /// <summary>
        /// Initializes a new instance of the <see cref="PivotCollectionBuilder"/> class.
        /// </summary>
        /// <param name="table">The table.</param>
        public PivotCollectionBuilder(PivotTable table)
        {
            this.table = table;
        }

        /// <summary>
        /// Creates the collection.
        /// </summary>
        /// <param name="publishFolder">The publish folder.</param>
        /// <param name="convertedImageFolder">The converted image folder.</param>
        /// <param name="deepZoomCollection">The deep zoom collection.</param>
        /// <param name="pivotCollectionFileName">Name of the pivot collection file.</param>
        /// <param name="collectionName">Name of the collection.</param>
        [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)]
        public void CreateCollection(string publishFolder, string convertedImageFolder, string deepZoomCollection, string pivotCollectionFileName, string collectionName)
        {
            CreateCollection(string.Empty, publishFolder, convertedImageFolder, deepZoomCollection, pivotCollectionFileName, collectionName);
        }

        /// <summary>
        /// Creates the collection.
        /// </summary>
        /// <param name="workingFolder">The working folder. Used to find images.</param>
        /// <param name="publishFolder">The publish folder.</param>
        /// <param name="convertedImageFolder">The converted image folder.</param>
        /// <param name="deepZoomCollection">The deep zoom collection.</param>
        /// <param name="pivotCollectionFileName">Name of the pivot collection file.</param>
        /// <param name="collectionName">Name of the collection.</param>
        [SecurityPermission(SecurityAction.LinkDemand, Unrestricted = true)]
        public void CreateCollection(string workingFolder, string publishFolder, string convertedImageFolder, string deepZoomCollection, string pivotCollectionFileName, string collectionName)
        {
            publishFolder = Path.IsPathRooted(publishFolder) ? publishFolder : Path.Combine(workingFolder, publishFolder);
            convertedImageFolder = Path.Combine(publishFolder, convertedImageFolder);
            string deepZoomCollectionFolder = Path.Combine(publishFolder, deepZoomCollection);
            pivotCollectionFileName = Path.Combine(publishFolder, pivotCollectionFileName);

            PivotXmlBuilder xmlBuilder = new PivotXmlBuilder()
            {
                ImageBase = string.Concat(deepZoomCollection, ".xml")
            };

            PivotXml xml = xmlBuilder.CreatePivotXml(this.table);
            xml.Name = collectionName;

            var converter = new ImageConverter(convertedImageFolder);
            var converteredImages = new List<string>();
            foreach (var dataRow in this.table.DataRows)
            {
                string fileName = Path.IsPathRooted(dataRow.Cells["Image Location"].Value) ?
                    dataRow.Cells["Image Location"].Value : Path.Combine(workingFolder, dataRow.Cells["Image Location"].Value);
                string newImage = converter.ConvertImage(fileName);
                converteredImages.Add(newImage);
            }

            var creator = new DeepZoomCreator();
            creator.CreateCollection(converteredImages, deepZoomCollectionFolder);

            xml.WriteXml(pivotCollectionFileName);
        }
    }
}
